/*  File Name	: FirewallSetting.h
 *  Author		: Javie Pong
 *	 E-mail		: 823960168@qq.com
 *  Created on	: 2012-10-1
 *  Description:
 */

#include "MikrotikAPITypes.h"
using namespace std;

Sentence::Sentence()
{
	this->strWords.clear();
	this->returnType = NONE;
}

Sentence::~Sentence()
{
}

void Sentence::SetReturnType(int returnTypeIn)
{
	returnType = returnTypeIn;
}

int Sentence::GetReturnType()
{
	return returnType;
}

void Sentence::AddWord(const std::string &strWordToAdd)
{
	strWords.push_back(strWordToAdd);
}

void Sentence::Clear()
{
	strWords.clear();
	returnType = 0;
}

int Sentence::Length()
{
	return strWords.size();
}

std::string Sentence::operator[](int index)
{
	return strWords[index];
}

std::string Sentence::GetWord(int index)
{
	return strWords[index];
}

void Sentence::Print()
{
	std::string tmp="";
	for (int i = 0; i <(int)strWords.size(); ++i)
	{
		tmp +=(strWords[i]+"\t");
	}
	printf("%s\n", tmp.c_str());
}

void Sentence::GetMap(StrMap &sentenceMap)
{
	for (int i = 0; i <(int)strWords.size(); i++)
	{
		string tmpDataString = strWords[i];
		vector<string> dataStrings;
		Tokenize(tmpDataString, dataStrings, "=");

		if (returnType == NONE && dataStrings.size() > 1)
		{
			sentenceMap.insert(make_pair(dataStrings[0], dataStrings[1]));
		}
	}
}

void Sentence::Tokenize(const string &str, StrVector &tokens,
		const string &delimiters)
{
	// Skip delimiters at beginning.
	string::size_type lastPos = str.find_first_not_of(delimiters, 0);
	// Find first "non-delimiter".
	string::size_type pos = str.find_first_of(delimiters, lastPos);

	while (string::npos != pos || string::npos != lastPos)
	{
		// Found a token, add it to the vector.
		tokens.push_back(str.substr(lastPos, pos - lastPos));
		// Skip delimiters.  Note the "not_of"
		lastPos = str.find_first_not_of(delimiters, pos);
		// Find next "non-delimiter"
		pos = str.find_first_of(delimiters, lastPos);
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////

Block::Block()
{
	sentences.clear();
}

Block::~Block()
{
}

int Block::Length()
{
	return sentences.size();
}

void Block::Clear()
{
	sentences.clear();
}

Sentence Block::operator[](int index)
{
	return sentences[index];
}

void Block::AddSentence(const Sentence &sentence)
{
	sentences.push_back(sentence);
}

void Block::Print()
{
	for (int i = 0; i <(int)sentences.size(); ++i)
	{
		sentences[i].Print();
	}
}
